import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

import { Geolocation,GeolocationOptions} from '@ionic-native/geolocation';


@IonicPage()
@Component({
  selector: 'page-geolocation',
  templateUrl: 'geolocation.html',
})
export class GeolocationPage {

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                public geolocation: Geolocation) {
    }

    public locData = {
        "title":'定位中...',
        "isShow": false,
        "lat": 0,
        "lon": 0,
        "accuracy": 0
    }

    doLocation(){
        const options: GeolocationOptions = {
            maximumAge: 3000,
            timeout: 20000,
            enableHighAccuracy: true
        }
        this.geolocation.getCurrentPosition(options).then((position) => {
            // 获取成功
            console.log('获取定位成功 -> '+JSON.stringify(position));
            var crd = position.coords;
            this.locData.title = '定位成功！';
            this.locData.isShow = true;
            this.locData.lat = crd.latitude;
            this.locData.lon = crd.longitude;
            this.locData.accuracy = crd.accuracy;

        }, (err) => {
            console.log('获取定位失败 -> ' + JSON.stringify(err));
            if(err.code == 3){
                this.locData.title = '超时！';
            }else{
                this.locData.title = '定位失败！';
            }
            this.locData.isShow = false;
        });
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad LocationPage');
        this.doLocation();
    }

}
